package com.parktech.sun.web.controller.management.report;

import com.parktech.sun.domain.model.Constants;
import com.parktech.sun.domain.model.report.KeyTechnicReformInvestStatus;
import com.parktech.sun.domain.model.sunhz.Enterprise;
import com.parktech.sun.domain.repository.Page;
import com.parktech.sun.domain.service.report.KeyTechnicReformInvestStatusService;
import com.parktech.sun.domain.service.sunhz.EnterpriseService;
import com.parktech.sun.web.controller.management.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created by IntelliJ IDEA.
 * User: guo
 * Date: 11-1-16
 * Time: 下午8:52
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/management/report")
public class KeyTechnicReformInvestStatusController extends BaseController{
    @Autowired
    private KeyTechnicReformInvestStatusService keyTechnicReformInvestStatusService;
    @Autowired
    private EnterpriseService enterpriseService;
    private final String redirectListURL = "redirect:listKeyTechnicReformInvestStatuses";

    @RequestMapping(value = "/listKeyTechnicReformInvestStatuses", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView listKeyTechnicReformInvestStatuses(HttpServletRequest request) {
        Integer currentPageNo = request.getParameter("pageNo") != null ? Integer.parseInt(request.getParameter("pageNo").trim()) : 1;
        ModelAndView modelAndView = new ModelAndView("management/report/keyTechnicReformInvestStatusList");

        List fieldColumns = new ArrayList();
        List searchValues = new ArrayList();
        if (request.getParameter("searchKeyName") != null) {
            fieldColumns.add("name");
            searchValues.add(request.getParameter("searchKeyName"));
        }
        Page<KeyTechnicReformInvestStatus> returnPage = new Page<KeyTechnicReformInvestStatus>(Constants.PAGINATION_PAGE_SIZE, currentPageNo);
        returnPage.setTotalCount(keyTechnicReformInvestStatusService.getTotalSizeWithSimilarCondition(KeyTechnicReformInvestStatus.class, fieldColumns, searchValues));
        returnPage.setResult(keyTechnicReformInvestStatusService.getPaginationObjectsWithSimilarCondition(KeyTechnicReformInvestStatus.class, currentPageNo, Constants.PAGINATION_PAGE_SIZE, fieldColumns, searchValues, null, null));

        return modelAndView.addObject(returnPage);
    }


    //    @ModelAttribute      @RequestParam
    @RequestMapping(value = "/manageKeyTechnicReformInvestStatus", method = RequestMethod.GET)
    public String manageKeyTechnicReformInvestStatus(Model model, HttpServletRequest request) {
        Long keyTechnicReformInvestStatusID = request.getParameter("keyTechnicReformInvestStatusID") != null ? Long.parseLong(request.getParameter("keyTechnicReformInvestStatusID").trim()) : null;
        if (keyTechnicReformInvestStatusID == null)     // request to add the new contact keyTechnicReformInvestStatus object
        {
            model.addAttribute(new KeyTechnicReformInvestStatus());
        } else       // request to update the certain keyTechnicReformInvestStatus object
        {
            KeyTechnicReformInvestStatus keyTechnicReformInvestStatus = keyTechnicReformInvestStatusService.getBeanByID(keyTechnicReformInvestStatusID);
            model.addAttribute(keyTechnicReformInvestStatus);
        }
        return "management/report/keyTechnicReformInvestStatusManage";
    }

    @RequestMapping(value = "/saveManageKeyTechnicReformInvestStatus", method = RequestMethod.POST)
    public String saveManageKeyTechnicReformInvestStatus(@ModelAttribute KeyTechnicReformInvestStatus keyTechnicReformInvestStatus, HttpServletRequest request) {
        Long keyTechnicReformInvestStatusID = keyTechnicReformInvestStatus.getId();
        keyTechnicReformInvestStatusService.saveOrUpdateBean(keyTechnicReformInvestStatus);
        if (keyTechnicReformInvestStatusID == null) {
            // add the success to create message.
            saveSuccessMessage(request,"创建成功！");
        } else {
            // add the success to edit message.
            saveSuccessMessage(request,"修改成功！");
        }

        return "management/report/keyTechnicReformInvestStatusManage";
    }

    @RequestMapping(value = "/deleteKeyTechnicReformInvestStatuss", method = RequestMethod.POST)
    public String deleteKeyTechnicReformInvestStatuss(HttpServletRequest request) {
        String[] keyTechnicReformInvestStatusIDArray = request.getParameter("selectedKeyTechnicReformInvestStatusIDArray") == null ? new String[0] : request.getParameter("selectedKeyTechnicReformInvestStatusIDArray").trim().split(",");
        for (String keyTechnicReformInvestStatusID : keyTechnicReformInvestStatusIDArray) {
            keyTechnicReformInvestStatusService.removeBean(Long.valueOf(keyTechnicReformInvestStatusID.trim()));
        }
        return redirectListURL;
    }

    @ModelAttribute("enterpriseMap")
    public Map<Long, String> getAllEnterpriseMap() {
        Map<Long, String> enterpriseMap = new TreeMap<Long, String>();
        List<Enterprise> enterpriseList = enterpriseService.getPaginationObjectsWithSimilarCondition(Enterprise.class, 1, 6666, null, null, null, null);
        for (Enterprise enterprise : enterpriseList) {
            enterpriseMap.put(enterprise.getId(), enterprise.getName());
        }
        return enterpriseMap;
    }

}
    


